package com.goicter.commons.utils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Pattern;

import org.apache.commons.validator.EmailValidator;

public final class CommonUtils {

	// 纸条审核关键词
	public static final String PAPERLOVE__VERIFY_KEYWORDS = "约炮,做爱,口交,性交,阴道,射精,性饥渴,强奸,共产,微信,weixin,陌陌,藏头,企鹅,jb,鸡巴,草泥马,操你,草你,狗杂种,微博,微薄,邓小平,江泽民,胡锦涛,习近平,法轮,李洪志,反共,共匪,达赖,天安门,中共,朋友网,世纪佳缘,qq,qQ,QQ,Qq,q聊,Q聊,扣扣,秋秋,求求,球球,腾讯,企鹅号,wei信,微xin,网名,见面,地点,地址,联系,加我";
	// 数字字符
	public static final String PAPERLOVE__VERIFY_NUMBER = "一二三四五六七八九零１２３４５６７８９０0123456789㈠㈡㈢㈣㈤㈥㈦㈧㈨⒈⒉⒊⒋⒌⒍⒎⒏⒑ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ零〇领另灵令玲拎凌捌柒玖叁岭铃龄一①壹亿依咦伊意易衣妖要幺咬药腰摇二贰儿而②尔耳爱饵洱三③伞参散馓山闪潵四丝死肆④是思斯似私司五吾唔伍⑤吴物乌屋午舞武六陆录溜留刘⑥遛了柳路七⑦柒气其期妻器齐起八霸捌⑧爸把吧巴拔扒罢九久就玖酒⑨旧救舅究";
	// 纸条审核正则1
	public static final Pattern pattern1 = Pattern.compile("[一二三四五六七八九零0123456789１２３４５６７８９０ ,.，。#&$_?㈠㈡㈢㈣㈤㈥㈦㈧㈨⒈⒉⒊⒋⒌⒍⒎⒏⒑ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ零〇领另灵令玲拎凌捌柒玖叁岭铃龄一①壹亿依咦伊意易衣妖要幺咬药腰摇二贰儿而②尔耳爱饵洱三③伞参散馓山闪潵四丝死肆④是思斯似私司五吾唔伍⑤吴物乌屋午舞武六陆录溜留刘⑥遛了柳路七⑦柒气其期妻器齐起八霸捌⑧爸把吧巴拔扒罢九久就玖酒⑨旧救舅究]{6}");
	// 纸条审核正则2
	public static final Pattern pattern2 = Pattern.compile("[0-9]{1,}[^0-9]{1,}[0-9]{1,}[^0-9]{1,}[0-9]{1,}[^0-9]{1,}");
	// 纸条审核正则3
	public static final Pattern pattern3 = Pattern.compile("[a-zA-Z0-9]{5,}@[a-zA-Z0-9]{2,9}[.][a-zA-Z0-9]{1,3}");
	// 纸条审核正则4
	public static final Pattern pattern4 = Pattern.compile("[a-zA-Z]{4}");

	/**
	 * 字符串是不是包含敏感词或者联系信息
	 * 
	 * @param content
	 * @return
	 */
	public static boolean isCanSendContent(String content) {
		boolean ifContentOK = true;
		return ifContentOK;
	}

	/**
	 * 验证字符串是否为NULL, "null"或EMPTY
	 * 
	 * @param src
	 * @return 为空返回true,不为空返回false
	 */
	public static boolean verifyNull(String src) {
		boolean result = false;
		if (src == null) {
			result = true;
		} else if ("".equals(src.trim())) {
			result = true;
		} else if ("null".equalsIgnoreCase(src.trim())) {
			result = true;
		}
		return result;
	}

	/**
	 * 验证手机号码是否是正确
	 * 
	 * @param mobile
	 *            手机号码
	 * @return 正确返回true, 不正确返回false
	 */
	public static boolean verifyMobile(String mobile) {
		if (!verifyNull(mobile)) {
			final String mobileRegexp = "^(1[3458][0-9]{9})$";
			final Pattern pattern = Pattern.compile(mobileRegexp);
			if (pattern.matcher(mobile).find()) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 验证E-mail格式是否正确
	 * 
	 * @param email
	 *            E-mail地址
	 * @return 正确返回true, 不正确返回false
	 */
	public static boolean verifyEmail(String email) {
		if (!verifyNull(email)) {
			return EmailValidator.getInstance().isValid(
					email.toLowerCase().trim());
		}
		return false;
	}

	/**
	 * 验证密码格式是否正确
	 * 
	 * @param password
	 *            密码
	 * @return
	 */
	public static boolean verifyPassword(String password) {
		if (!verifyNull(password)) {
			return password.matches("^[A-Za-z0-9]{6,20}$");
		}
		return false;
	}

	/**
	 * 验证昵称格式是否正确
	 * 
	 * @param nickname
	 *            昵称
	 * @return 正确返回true, 不正确返回false
	 */
	public static boolean verifyNickname(String nickname) {
		if (!verifyNull(nickname)) {
			if (nickname.matches("[\\u4e00-\\u9fa5\\w]{2,16}")) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 根据年，月，日获得时间
	 * 
	 * @param year
	 *            年
	 * @param month
	 *            月(值：1-12)
	 * @param day
	 *            日
	 * @return
	 */
	public static Date getDate(int year, int month, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DAY_OF_MONTH, day);

		return calendar.getTime();
	}

	/**
	 * 替换图片URL中的"-xx"
	 * 
	 * @param url
	 * @return
	 */
	public static String replacePhotoUrl(String url) {
		if (verifyNull(url)) {
			return "";
		}
		return url.replaceAll("-\\d{2,}(\\.{0,1})", "$1");
	}

	/**
	 * 使用(yyyy-MM-dd HH:mm:ss)模式格式化日期.
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDate(Object date) {
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String formatDate(Object date, String format) {
		if (date == null) {
			return "";
		}

		if (date instanceof Date) {
			return new SimpleDateFormat(format).format(date);
		}

		return date.toString();
	}

	public static int parseInt(Object obj) {
		if (obj == null) {
			return 0;
		}

		try {
			return Integer.parseInt(obj.toString());
		} catch (NumberFormatException e) {
			return 0;
		}
	}

	@SuppressWarnings("static-access")
	public static String getSendTimeStr(Date sendTime) {
		if (sendTime == null) {
			return "";
		}
		Date date = new Date();// 取时间
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm");
		String dateString = formatter.format(date);
		String sendMailDate = formatter.format(sendTime);
		String hDate = formatter2.format(sendTime);
		// 今天
		if (dateString.equals(sendMailDate)) {
			return hDate;
		}
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, -1);
		Date yesterdate = calendar.getTime();
		String yesterDay = formatter.format(yesterdate);
		if (yesterDay.equals(sendMailDate)) {
			return "昨天 " + hDate;
		}
		return sendMailDate + " " + hDate;
	}

	/**
	 * 获取表名
	 * 
	 * @param tableName
	 *            表前缀名如UserInfo
	 * @param tableAmounts
	 *            表数量 100 or 1000
	 * @param id
	 *            id如123456789
	 * @return 返回UserInfo_89 or UserInfo_089
	 */
	public static String getTableName(String tableName, int tableAmounts, int id) {
		return getTableName(tableName, tableAmounts, id, String.valueOf(tableAmounts).length() - 1);
	}

	/**
	 * 获取表名
	 * 
	 * @param tableName
	 *            表前缀名如UserInfo
	 * @param tableAmounts
	 *            表数量 5 or 10 or 100 ...
	 * @param id
	 *            id 如123456789
	 * @param bit
	 *            格式化位数 1 or 2...
	 * @return 返回UserInfo_4 UserInfo_9 UserInfo_89 UserInfo_04 UserInfo_09
	 *         UserInfo_89
	 */
	public static String getTableName(String tableName, int tableAmounts,
			int id, int bit) {
		return tableName + "_" + String.format("%0" + bit + "d", id % tableAmounts, bit);
	}

}
